package com.fqcar.fqdf.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Response;
import com.fqcar.fqdf.R;
import com.fqcar.fqdf.ReportingProblems.ReportingProblemsActivity;
import com.fqcar.fqdf.adapter.HistoryCarListAdapter;
import com.fqcar.fqdf.adapter.HistoryImgListAdapter;
import com.fqcar.fqdf.core.SwipLoadMoreActivity;
import com.fqcar.fqdf.entity.HistoryCarListBean;
import com.fqcar.fqdf.service.RequestOptService;
import com.fqdf.framework.android.net.response.MidListRespBean;
import com.fqdf.framework.android.net.response.NetPageInfoBeanWrapper;
import com.fqdf.framework.android.util.Constants;
import com.fqdf.framework.android.view.LoadMoreListView;
import com.fqdf.framework.android.view.TwoSwipeRefreshLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * Created by lvsidong on 2017/10/30.
 * <p>
 * 历史处理列表
 */

public class HistoryCarListActivity extends SwipLoadMoreActivity {

    @BindView(R.id.no_data_iv)
    TextView noDataIv;
    @BindView(R.id.no_data_layout)
    NestedScrollView noDataLayout;
    @BindView(R.id.list)
    LoadMoreListView list;
    @BindView(R.id.swiprefresh)
    TwoSwipeRefreshLayout mSwipeRefreshLayout;

    private HistoryCarListAdapter historyCarListAdapter;
    private MidListRespBean carRespBean;

//    private HistoryImgListAdapter historyImgListAdapter;

    private String carLicense;
    private int carId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        ButterKnife.bind(this);

        carId = getIntent().getIntExtra(Constants.BUNDLE_DATA, 0);
        carLicense = getIntent().getStringExtra(Constants.BUNDLE_DATA_EXT);
        initMyView();
    }

    private void initMyView() {
//        historyImgListAdapter = new HistoryImgListAdapter(this);
        historyCarListAdapter = new HistoryCarListAdapter(this);
        list.setAdapter(historyCarListAdapter);
        initSwipLoadMoreView(list, mSwipeRefreshLayout, noDataLayout);
        // 进行刷新
        refreshDataAnim();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        refreshDataAnim();
    }

    private void getHistoryCarListPageNum(int carId, int pageNum) {
        RequestOptService.getHistoryCarListPageNum(getApplicationContext(), carId, pageNum, new Response.Listener<NetPageInfoBeanWrapper<HistoryCarListBean>>() {
            @Override
            public void onResponse(NetPageInfoBeanWrapper<HistoryCarListBean> response) {
                closeSwipRefreshAnim();
                if (handleResponse(response)) {
                    return;
                }
//                HistoryCarListRespBean historyCarListRespBean = response.getModelData();
                carRespBean = response.getPageInfo();

                handleLoadMore(carRespBean);
//                historyCarListAdapter.setList(historyCarListRespBean.getList());
//                historyCarListAdapter.notifyDataSetChanged();

            }
        }, getDefaultErrorListener());
    }

    @OnItemClick(R.id.list)
    public void onItemClick(int position) {
//        CarDetailEntity carDetailEntity = searchCarAdapter.getItem(position);
//        Intent controlIntent = new Intent(HistoryCarListActivity.this, StartDealActivity.class);
//        controlIntent.putExtra(Constants.BUNDLE_DATA, carDetailEntity.getCarId());
//        startActivity(controlIntent);
    }

    @OnClick({R.id.back_iv,R.id.history_car_list_reporting_tv})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.history_car_list_reporting_tv:
                Intent intent = new Intent(HistoryCarListActivity.this, ReportingProblemsActivity.class);
                intent.putExtra(Constants.BUNDLE_DATA,carId);
                intent.putExtra(Constants.BUNDLE_DATA_EXT,carLicense);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void loadMoreData() {
        getHistoryCarListPageNum( carId,carRespBean.getPageNum() + 1);
    }

    @Override
    public void onRefresh() {
        getHistoryCarListPageNum(carId,1);
    }
}
